package com.vote.business.service.impl;

import java.util.List;
import java.util.Objects;

import com.vote.business.domain.VoteInfo;
import com.vote.business.domain.vo.UserVoteRecordVO;
import com.vote.business.domain.vo.VoteInfoVO;
import com.vote.business.mapper.VoteInfoMapper;
import com.vote.business.mapper.VoteOptionMapper;
import com.vote.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vote.business.mapper.UserVoteRecordMapper;
import com.vote.business.domain.UserVoteRecord;
import com.vote.business.service.IUserVoteRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户投票记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@Service
public class UserVoteRecordServiceImpl implements IUserVoteRecordService 
{
    @Autowired
    private UserVoteRecordMapper userVoteRecordMapper;

    @Autowired
    private VoteInfoMapper voteInfoMapper;

    @Autowired
    private VoteOptionMapper voteOptionMapper;
    /**
     * 查询用户投票记录
     * 
     * @param recordId 用户投票记录主键
     * @return 用户投票记录
     */
    @Override
    public UserVoteRecord selectUserVoteRecordByRecordId(Long recordId)
    {
        return userVoteRecordMapper.selectUserVoteRecordByRecordId(recordId);
    }

    /**
     * 查询用户投票记录列表
     * 
     * @param userVoteRecord 用户投票记录
     * @return 用户投票记录
     */
    @Override
    public List<UserVoteRecord> selectUserVoteRecordList(UserVoteRecord userVoteRecord)
    {
        return userVoteRecordMapper.selectUserVoteRecordList(userVoteRecord);
    }

    /**
     * 新增用户投票记录
     * 
     * @param userVoteRecord 用户投票记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertUserVoteRecord(UserVoteRecord userVoteRecord)
    {
        //验证投票状态
        VoteInfo voteInfo =voteInfoMapper.selectVoteInfoByVoteId(userVoteRecord.getVoteId());
        if(voteInfo==null){
            AjaxResult.error("投票信息不存在");
        }
        if (voteInfo != null && !Objects.equals(voteInfo.getStatus(), VoteInfo.STATUS_PUBLISHED)) {
                AjaxResult.error("该投票未发布或已结束");
        }
        //检查是否已投票
        if(hasVoted(userVoteRecord.getVoteId(),userVoteRecord.getUserId())){
            AjaxResult.error("您已参与过该投票");
        }
        userVoteRecordMapper.insertUserVoteRecord(userVoteRecord);
        voteOptionMapper.incrementVoteCount(userVoteRecord.getOptionId());
        return AjaxResult.success("投票成功");
    }

    /**
     * 修改用户投票记录
     * 
     * @param userVoteRecord 用户投票记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUserVoteRecord(UserVoteRecord userVoteRecord)
    {
        if(userVoteRecord.getOptionId()>0&&userVoteRecord.getOldOptionId()>0&&!Objects.equals(userVoteRecord.getOptionId(), userVoteRecord.getOldOptionId())){
            voteOptionMapper.incrementVoteCount(userVoteRecord.getOptionId());
            voteOptionMapper.updateVoteOptionCount(userVoteRecord.getOldOptionId());
        }
        return userVoteRecordMapper.updateUserVoteRecord(userVoteRecord);
    }

    /**
     * 批量删除用户投票记录
     * 
     * @param recordIds 需要删除的用户投票记录主键
     * @return 结果
     */
    @Override
    public int deleteUserVoteRecordByRecordIds(Long[] recordIds)
    {
        return userVoteRecordMapper.deleteUserVoteRecordByRecordIds(recordIds);
    }

    /**
     * 删除用户投票记录信息
     * 
     * @param recordId 用户投票记录主键
     * @return 结果
     */
    @Override
    public int deleteUserVoteRecordByRecordId(Long recordId)
    {
        return userVoteRecordMapper.deleteUserVoteRecordByRecordId(recordId);
    }
    /**
     * 获取投片记录信息
     * @param recordId
     * @return
     */
    @Override
    public UserVoteRecordVO selectUserVoteRecordDtoById(Long recordId) {
        return userVoteRecordMapper.selectUserVoteRecordDtoById(recordId);
    }

    @Override
    public boolean hasVoted(Long voteId, Long userId) {
        return userVoteRecordMapper.judgeVoted(voteId,userId)>0;
    }

    @Override
    public int selectParticipantCount(Long voteId) {
        return userVoteRecordMapper.selectParticipantCount(voteId);
    }

    @Override
    public int selectTotalVotes(Long voteId) {
        return userVoteRecordMapper.selectTotalVotes(voteId);
    }

    /**
     * 查询用户投票记录列表（带关联信息）
     * @param record 查询条件
     * @return 投票记录列表
     */
    @Override
    public List<UserVoteRecordVO> selectUserVoteRecordDtoList(UserVoteRecordVO record) {
        return userVoteRecordMapper.selectUserVoteRecordDtoList(record);
    }
    /**
     * 获取投片记录信息
     * @param userVoteRecordVO
     * @return
     */
    @Override
    public UserVoteRecordVO selectUserVoteRecordDtoByUId(UserVoteRecordVO userVoteRecordVO){
        return userVoteRecordMapper.selectUserVoteRecordDtoByUId(userVoteRecordVO);
    }
}
